package com.gainsoft.grevocabuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class WordList
{
    public static final String DATABASE_NAME = "word";
    public static final String WORD_TABLE = "word";
    public static final String ID_COL = "_id";
    public static final String WORD_COL = "word";
    public static final String MEANING_COl = "meaning1";
    public static final String LERANINGINDEX_COL = "learningIndex";
    public static final int DATABASE_VERSION = 1;
    public static final String DB_PATH = "/data/data/com.gainsoft.grevocabuilder/databases/";
    private static int totalCountOfWord = 0;
    private int id_;

    private SQLiteDatabaseAdapter mSQLiteDatabaseAdapter;
    private SQLiteDatabase db;
    public Context mContext;
    public static WordList wordSingletonInstance = null;

    public static int getTotalCount(Context _context)
    {
	Cursor c = null;
	try
	{
	    if (totalCountOfWord == 0)
	    {
		c = getInstance(_context).db.rawQuery(
			"SELECT COUNT(word) as word_count   FROM  "
				+ WORD_TABLE, null);
		c.moveToFirst();
		totalCountOfWord = c.getInt(c.getColumnIndex("word_count"));
	    }

	} catch (Exception e)
	{
	    Log.e("getTotalCount", e.toString());
	}
	return totalCountOfWord;
    }

    private WordList(Context _context)
    {

	this.mContext = _context;
	initializeDatabase();
    }

    public static WordList getInstance(Context _context)
    {
	if (wordSingletonInstance == null)
	{
	    wordSingletonInstance = new WordList(_context);
	}
	return wordSingletonInstance;
    }

    private SQLiteDatabase initializeDatabase()
    {
	mSQLiteDatabaseAdapter = SQLiteDatabaseAdapter.getInstance(mContext,
		DATABASE_NAME);
	if (!SQLiteDatabaseAdapter.checkDatabase(mContext, DATABASE_NAME))
	{
	    db = mSQLiteDatabaseAdapter.getDatabase();

	} else
	{
	    db = mSQLiteDatabaseAdapter.getReadableDatabase();

	}
	return db;
    }

    public static Word getWordById(int id, Context _context)
    {
	WordList wordList = getInstance(_context);
	Word word = new Word();
	String[] args =
	{ Integer.toString(id) };

	Cursor c;
	try
	{
	    c = wordList.db.rawQuery("SELECT * FROM  " + WORD_TABLE + "  "
		    + " WHERE  " + ID_COL + " = ?", args);
	    c.moveToFirst();
	    word.setId_(c.getInt(c.getColumnIndex(ID_COL)));
	    word.setWord_(c.getString(c.getColumnIndex(WORD_COL)));
	    word.setMeaning_(c.getString(c.getColumnIndex(MEANING_COl)));
	    word.setLearningIndex_(c.getInt(c.getColumnIndex(LERANINGINDEX_COL)));
	} catch (Exception e)
	{
	    Log.e("getWordById", e.toString());
	}
	return word;
    }

    public static List<Word> getRandomListofWord()
    {
	List<Word> wordList = new ArrayList<Word>();
	for (int i = 0; i < 4; i++)
	{
	    wordList.add(getRandomWord());
	}

	return wordList;
    }

    public static Word getRandomWord()
    {
	Word word = null;
	Random rand = new Random();
	word = getWordById(
		rand.nextInt(getTotalCount(VocaBuilder.context)) + 1,
		VocaBuilder.context);

	return word;
    }

    public static Cursor getWordCursorByAlphabet(String alphabets,
	    Context _context)
    {
	Cursor c = null;
	alphabets = alphabets.toLowerCase();
	try
	{
	    if (alphabets == "all")
	    {
		c = getInstance(_context).db.rawQuery("SELECT * FROM  "
			+ WORD_TABLE + " order by  " + WORD_COL + "  ASC ,"
			+ LERANINGINDEX_COL, null);
	    } else
	    {
		c = getInstance(_context).db.rawQuery("SELECT * FROM  "
			+ WORD_TABLE + "  " + " WHERE  " + WORD_COL + " Like '"
			+ alphabets + "%' order by  " + WORD_COL + "  ASC ,"
			+ LERANINGINDEX_COL, null);
	    }

	} catch (Exception e)
	{
	    Log.e("getWordCursorByAlphabet", e.toString());
	}
	return c;
    }

}
